package proxy 
{
	import data.DataMonitor;
	import debug.ASSERT;
	import nsdecl.ns_control_model;
	import nsdecl.ns_fileurl_const;
	import nsdecl.ns_vname_decl;
	import common.IData;
	import common.Struct;
	import common.StructTime;
	import pool.Pool;
	import vndecl.*;
	/**
	 * 我靠需要个模板才行了啊卧槽
	 * @author anod
	 */
	internal class StructTimeProxy implements IDataProxy 
	{
		use namespace ns_fileurl_const;
		use namespace ns_control_model;
		use namespace ns_vname_decl;
		
	///-------------------------------------
		private var refStruct:Struct;
		private var mDirty:Boolean;
		private var mIdGen:int;
		
		public function StructTimeProxy( struct:Struct ) 
		{
			super();
			refStruct = struct;
			DataMonitor.updateData( refTimesegVector, vname );
		}
		
		private function get refTimesegVector():Vector.<StructTime>
		{
			return refStruct.mTimesegVector;
		}
		
		/* INTERFACE proxy.IDataProxy */
		
		public function init():void
		{
			mIdGen = refTimesegVector.length;
		}
		
		public function get vname():String 
		{
			return VNGM.PROXY_TIMESEG;
		}
		
		public function get isDirty():Boolean
		{
			return mDirty;
		}
		internal function clearDirty():void
		{
			mDirty = false;
		}
		
		public function insert(v:IData):int 
		{
			ASSERT( v is StructTime, "只接受“班次”数据" );
			
			var newentry:StructTime;
			
			// 检查是否重复
			newentry = match( v as StructTime );
			if ( newentry != null ) return -1;
			
			// 补全数据
			newentry       = new StructTime( GenID() );
			newentry.name  = StructTime(v).name;
			newentry.start = StructTime(v).start;
			newentry.spend = StructTime(v).spend;
			newentry.size  = StructTime(v).size;
			
			// 加入到队列中
			(mDirty = true) && refTimesegVector.push( newentry ) && DataMonitor.notify( vname );
			return newentry.ID;
		}
		
		public function remove(id:int):int 
		{
			var index_to_del:int = -1;
			refTimesegVector.some(
				function( entry:StructTime, idx:int, db:Vector.<StructTime> ):Boolean
				{
					var find:Boolean = ( entry.ID == id );
					if ( find ) index_to_del = idx;
					return find;
				}
			);
			(id >= 0) && refTimesegVector.splice(index_to_del,1) && (mDirty = true) && DataMonitor.notify(  vname );
			return index_to_del < 0 ? index_to_del : id;
		}
		
		public function update( id:int, v:IData ):int 
		{
			ASSERT( v is StructTime, "只接受“班次”数据" );
			
			var entry:StructTime = getByID( id ) as StructTime;
			if ( entry == null ) return -1;
			
			entry.name  = StructTime(v).name;
			entry.start = StructTime(v).start;
			entry.spend = StructTime(v).spend;
			entry.size  = StructTime(v).size;
			
			(mDirty = true) && DataMonitor.notify( vname );
			return id;
		}
		
		public function getByID( id:int ):IData
		{
			ASSERT( id >= 0, "合法ID必定是非负数" );
			for each( var timeseg:StructTime in refTimesegVector )
			{
				if ( timeseg.ID == id )
					return timeseg;
			}
			return null;
		}
		
		/**
		 * 从已有的队列mStaffVector中找到和传入项想匹配的数据
		 * @param	v - 需要对应匹配的项
		 * @param   id - 如果非负，表示只匹配id
		 * @return  找到了想匹配的项，返回对应对象，否则返回null
		 */
		protected function match( v:StructTime ):StructTime
		{
			for each( var timeseg:StructTime in refTimesegVector )
			{
				if ( timeseg.name == v.name )
					return timeseg;
			}
			return null;
		}
		protected function GenID():int
		{
			return mIdGen++;
		}
	}

}